{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8dfa7ec1-4c10-40cd-9e2a-f66de09da2bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import pandas as pd\n",
    "from transformers import AutoTokenizer, AutoModelForSequenceClassification\n",
    "\n",
    "# ===== Step 1: Load Llama 3 Model & Tokenizer =====\n",
    "MODEL_NAME = \"meta-llama/Llama-3-7B\"  # Change if needed\n",
    "tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)\n",
    "model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)\n",
    "\n",
    "# Force CPU execution to avoid GPU issues\n",
    "device = torch.device(\"cpu\")\n",
    "model.to(device)\n",
    "\n",
    "# ===== Step 2: Load Excel Data =====\n",
    "file_path = \"/mnt/data/sentiment_data.xlsx\"  # Ensure this file is uploaded here\n",
    "df = pd.read_excel(file_path)\n",
    "\n",
    "# Ensure 'speech' column exists\n",
    "if 'speech' not in df.columns:\n",
    "    raise ValueError(\"The column 'speech' is missing from the dataset.\")\n",
    "\n",
    "# ===== Step 3: Function for Sentiment Classification =====\n",
    "def classify_sentiment(text):\n",
    "    prompt = (\n",
    "        \"분석 대상: 주어진 문장에서 기초단체의원에 대한 감정을 분류하세요.\\n\"\n",
    "        \"규칙:\\n\"\n",
    "        \"- 긍정적인 언어: 긍정적인 내용을 포함하는 경우.\\n\"\n",
    "        \"- 중립적인 언어: 감정적 편향이 거의 없거나 사실을 서술하는 경우.\\n\"\n",
    "        \"- 부정적인 언어: 부정적인 내용을 포함하는 경우.\\n\\n\"\n",
    "        \"문장:\\n\"\n",
    "        f\"{text}\\n\"\n",
    "        \"위 문장의 감정 분류 결과를 제공합니다:\"\n",
    "    )\n",
    "\n",
    "    # Tokenizing input text\n",
    "    inputs = tokenizer(prompt, return_tensors=\"pt\", truncation=True, padding=True).to(device)\n",
    "\n",
    "    # Perform inference without computing gradients\n",
    "    with torch.no_grad():\n",
    "        outputs = model(**inputs)\n",
    "        logits = outputs.logits\n",
    "        predicted_class = torch.argmax(logits, dim=1).item()\n",
    "\n",
    "    # Sentiment Mapping\n",
    "    if predicted_class == 0:\n",
    "        return 1  # Positive\n",
    "    elif predicted_class == 1:\n",
    "        return 0  # Neutral\n",
    "    else:\n",
    "        return -1  # Negative\n",
    "\n",
    "# ===== Step 4: Apply Sentiment Classification to Each Row =====\n",
    "df[\"sentiment\"] = df[\"speech\"].apply(classify_sentiment)\n",
    "\n",
    "# ===== Step 5: Save the Updated DataFrame =====\n",
    "output_file = \"/mnt/data/sentiment_classified.xlsx\"\n",
    "df.to_excel(output_file, index=False)\n",
    "\n",
    "# ===== Step 6: Display & Provide Download Link =====\n",
    "import ace_tools as tools\n",
    "tools.display_dataframe_to_user(name=\"Sentiment Classification Results\", dataframe=df)\n",
    "\n",
    "# Provide a download link\n",
    "print(f\"Download classified data: {output_file}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
